﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HtmlAgilityPack;
using FluentData;
using System.Text;
using System.Net;
namespace htmlTBK
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    Response.Clear();

        //    int pageNo = 1;
        //    string pageParam = "&s=0";
        //    if (int.TryParse("1", out pageNo) && pageNo > 1)
        //    {
        //        pageParam = "&s=" + (pageNo - 1) * 40;
        //    }

        //    HtmlWeb web = new HtmlWeb();
        //    web.UseCookies = false;
        //    web.NeedParse = false;
        //    web.OverrideEncoding = Encoding.GetEncoding("gb2312");
        //    web.PreRequest += req;
        //    //&sort=sale-desc&tab=mall&
        //    string key =Request.QueryString["key"]==null? "网站系统":Request.QueryString["key"];
        //    key = Server.UrlEncode(key);
        //    pageParam = "&sort=sale-desc&commend=all&search_type=item&sourceId=tb.index&spm=1.1000386.5803581.d4908513&initiative_id=tbindexz_20130703";
        //    //http://s8.taobao.com/search?q=%C5%AE%D7%B0&commend=all&search_type=item&sourceId=tb.index&spm=1.1000386.5803581.d4908513&initiative_id=tbindexz_20130703
        //    HtmlDocument hd = web.Load("http://s8.taobao.com/search?q=" + key + pageParam);
        //    string result = hd.Text;
        //    Response.Write(result);
        //    return;
        //    RegexModel m = new RegexModel();
        //    m.OriginText = result;
        //    m.Pattern = "id=\"list-content\".*?<div\\sclass=\"col-sub\">";
        //    m.SonPattern = "class=\"list-item\"(.*?)class=\"legend2\"";
        //    Dictionary<string, string> itemPattern = new Dictionary<string, string>();
        //    itemPattern.Add("itemid", "data-item=\"(?<itemid>.*?)\"");
        //    itemPattern.Add("itemname", "title=\"(?<itemname>.*?)\"");
        //    itemPattern.Add("itemimgurl", "lazyload=\"(?<itemimgurl>.*?)\"");
        //    //itemPattern.Add("comment", "");
        //    itemPattern.Add("seller", "class=\"seller\"><a.*?>(?<seller>.*?)</a>");
        //    itemPattern.Add("loc", "class=\"loc\">(?<loc>.*?)</span>");
        //    itemPattern.Add("price", "class=\"price\"><em>(?<price>.*?)</em>");
        //    itemPattern.Add("dealing", "</em>\\s<span >(?<dealing>.*?)</span>");
        //    itemPattern.Add("fee","class=\"fee\">(?<fee>.*?)</span>");
        //    m.ItemPattern = itemPattern;
        //    var rs = m.GetAllItems();
        //    if (rs.Count>0)
        //    {
        //        for (int i = 0; i < rs.Count; i++)
        //        {
        //            foreach (var item in rs[i].Keys)
        //            {
        //                Response.Write(item + "==>" + rs[i][item][0]+"<br/>");
        //            }
        //            Response.Write("<br/><br/>");
        //        }
        //    }
        //}
        private bool req(System.Net.HttpWebRequest request)
        {
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8");
            request.Headers.Add("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
            request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
            request.Headers.Add("Origin", "http://search.taobao.com");
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.Host = "search.taobao.com";
            request.Referer = "http://search.taobao.com/search?q=%B4%F3%C5%C6%D7%D3&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=1.1000386.5803581.d4908513";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31";
            return true;
        }

    }
}